package com.zhao.dao;

import com.zhao.bean.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

    @Repository
    public class StudentDao implements StudentDa {

        @Autowired
        JdbcTemplate jdbcTemplate;

        @Override
        public int addStudent(String number, String name, String gender, String clbum, String grade) {
            String sql = "  insert into students(number, name, gender, clbum, grade) values(?,?,?,?,?)";
            int add = jdbcTemplate.update(sql, number, name, gender, clbum, grade);
            return add;
        }

        @Override
        public int updateStudent(String unmber, String name, String gender, String clbum, String grade) {
            //根据姓名修改
            String sql = "update students set number=?,gender=?, clbum=?, grade=? where  name=? ";
            int update = jdbcTemplate.update(sql, unmber, gender, clbum, grade, name);
            return update;
        }

        @Override
        public int deleteStudent(String name) {
            String sql = "delete from students where name=?";
            int delete = jdbcTemplate.update(sql, name);
            return delete;
        }

        @Override
        public Students selectStudentName(String name) {
            String sql = "select * from students where name=?";
            Students students = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Students>(Students.class), name);
            return students;
        }

        @Override
        public List<Students> selectStudent() {
            String sql = "select * from students";
            List<Students> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Students>(Students.class));
            return students;
        }



    }

